<template>
	<view class="body">
		<form @submit="formSubmit">
			<!-- 头像 -->
					<view class="face-wapper">
						<image src="../../static/icos/default-face.png" class="face"></image>
					</view>
				<!-- 账号输入 -->
					<view class="info-wapper">
						<label class="words-lbl">账号</label>
						<input name="username" type="text" value="" class="input" placeholder="请输入电话号码" placeholder-class="graywords"/>
					</view>
					<!-- 密码输入 -->
					<view class="info-wapper" style="margin-top: 40upx;">
						<label class="words-lbl">密码</label>
						<input name="password" type="text" value="" password="true" class="input" placeholder="请输入密码" placeholder-class="graywords"/>
					</view>
				<!-- 登录按钮 -->
					<button type="primary"  form-type="submit" style="margin-top: 60upx;width: 90%;">登录</button>
		</form>
		<navigator url="../register/register">
			<view class="register">注册</view>
		</navigator>
			
				<!-- 第三方登录 -->
				<!-- 第三方登录H5不支持，所以隐藏掉 -->
				<!-- #ifndef H5 -->
					<view class="third-wapper">
						<view class="third-line">
							<view class="single-line">
								<view class="line"></view>
							</view>
							
							<view class="third-words">第三方账号登录</view>
							
							<view class="single-line">
								<view class="line"></view>
							</view>
						</view>
						
						<view class="third-icos-wapper">
							<!-- 5+app 用qq/微信/微博 登录 小程序用微信小程序登录 h5不支持 -->
							<!-- #ifdef APP-PLUS -->
								<image src="../../static/icos/weixin.png" data-logintype="weixin" @click="appOAuthLogin"  class="third-ico"></image>
								<image src="../../static/icos/QQ.png" data-logintype="qq" @click="appOAuthLogin"  class="third-ico" style="margin-left: 80upx;"></image>
								<image src="../../static/icos/weibo.png" data-logintype="sinaweibo" @click="appOAuthLogin" class="third-ico" style="margin-left: 80upx;"></image>
							<!-- #endif -->
							<!-- #ifdef MP-WEIXIN -->
								<button open-type='getUserInfo' @getuserinfo="wxLogin" class="third-btn-ico">
								</button>
							<!-- #endif -->
						</view>
					</view>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//登录的信息对象
				username:'',
				password:'',
				//接收登录成功后的用户信息
				userInfo:{},
				baseUrl:'http://192.168.42.83:3000/wx/api/'
			}
		},
		methods: {
			//提交登录信息
			//这是提交登录信息
		formSubmit(evt){
				console.log(evt)
				this.username =	evt.detail.value.username;
				this.password =	evt.detail.value.password;
				//发起请求  第一代版本无需校验
				uni.showLoading({
				    title: '正在登录'  
				}); 
			// uni.request({
			// //	url: 'http://127.0.0.1:3000/wx/api/login', 
			// 	url: 'http://192.168.42.83:3000/wx/api/login',
			// //	url: this.baseUrl + 'login',
			// 	method:"POST",
			// 	data: {   
			// 		username : this.username,
			// 		password : this.password
			// 	},
			// 	header: {
			// 		'content-type':'application/json'
			// 	},
			// 	success: (res) => {
				
			// 		console.log(res);	
			// 		uni.hideLoading();  
			// 		if(res.statusCode !== 200)
			// 		return uni.showToast({title: '登录失败'});
			// 		uni.showToast({
			// 			title: '登录成功',
			// 			duration:2000
			// 			});
			// 		this.userInfo = res.data.user;
			// 		console.log(this.userInfo);
			// 		//将登录返回后的信息进行缓存
			// 		uni.setStorageSync("globalUser", this.userInfo)
			// 		//跳转页面
			// 		uni.switchTab({ url: '/pages/me/me' });
			// 	}
			//  });
			
			// 云开发登录
			uniCloud.callFunction({
			    name:"userLog",
				data:{
					username : this.username,
					password : this.password,
				},
				success: (res) => {
					uni.hideLoading();
					if(res.result.affectedDocs != 1)
					  return uni.showToast({
							title: '登录失败',
							duration: 2000});	
								
						uni.showToast({
							title: '登录成功',
							duration: 2000});	
							console.log(res.result.data[0])
						this.userInfo = res.result.data[0];
						uni.setStorageSync("globalUser", this.userInfo)
						//跳转页面
						uni.switchTab({ url: '/pages/me/me' });
				},
				fail: (err) => {
					console.log(err);
							uni.hideLoading();
							uni.showToast({title: '登录失败'});
				}
			   
			});
			
				},
       //微信第三方登录
	   wxLogin(evt){
		   console.log("evt");
		   console.log(evt);
	   }
				
				
		}
	}
</script>
<style>
@import url("./registerLogin.css");
</style>
